package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品管理控制器
 */
@RestController
@RequestMapping("/admin/goods")
@Tag(name = "商品管理接口", description = "提供商品的增删改查功能")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 获取商品分页列表
     *
     * @param pageNum 页码
     * @param count   每页数量
     * @param keyword 关键词
     * @return 商品分页数据
     */
    @GetMapping("/list")
    @Operation(summary = "获取商品分页列表", description = "主要用于商品管理")
    public Result<PageDTO<GoodsDTO>> getGoodsList(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "count", required = false) Integer count,
            @RequestParam(value = "keyword", required = false) String keyword) {

        // 创建请求对象并正确设置参数
        PageRequest pageRequest = new PageRequest();

        // 处理页码，确保有效值
        pageRequest.setPageNum(pageNum != null && pageNum > 0 ? pageNum : 1);

        // 处理每页数量，确保有效值
        pageRequest.setCount(count != null && count > 0 ? count : 10);

        // 设置关键词
        pageRequest.setKeyword(keyword);

        log.info("获取商品分页列表, 请求参数: {}", pageRequest);
        PageDTO<GoodsDTO> pageDTO = goodsService.getGoodsPage(pageRequest);
        return Result.ok(pageDTO);
    }

    /**
     * 添加商品
     *
     * @param command 商品信息
     * @return 操作结果
     */
    @PostMapping
    @Operation(summary = "添加商品", description = "主要用于商品管理")
    public Result<Void> addGoods(@RequestBody GoodsCommand command) {
        log.info("添加商品, 请求参数: {}", command);
        goodsService.addGoods(command);
        return Result.ok();
    }

    /**
     * 修改商品
     *
     * @param command 商品信息
     * @return 操作结果
     */
    @PutMapping
    @Operation(summary = "修改商品", description = "主要用于商品管理")
    public Result<Void> updateGoods(@RequestBody GoodsCommand command) {
        log.info("修改商品, 请求参数: {}", command);
        goodsService.updateGoods(command);
        return Result.ok();
    }

    /**
     * 修改商品状态（上架/下架）
     *
     * @param goodsId 商品ID
     * @param status  商品状态
     * @return 操作结果
     */
    @PutMapping("/status/{goodsId}")
    @Operation(summary = "修改商品状态", description = "主要用于商品管理")
    public Result<Void> updateGoodsStatus(@PathVariable Long goodsId, @RequestParam Integer status) {
        log.info("修改商品状态, 商品ID: {}, 状态: {}", goodsId, status);
        goodsService.updateGoodsStatus(goodsId, status);
        return Result.ok();
    }

    /**
     * 删除商品
     *
     * @param goodsId 商品ID
     * @return 操作结果
     */
    @DeleteMapping("/{goodsId}")
    @Operation(summary = "删除商品", description = "主要用于商品管理")
    public Result<DeleteGoodsOperateDTO> deleteGoods(@PathVariable Long goodsId) {
        log.info("删除商品, 商品ID: {}", goodsId);
        DeleteGoodsOperateDTO operateDTO = goodsService.deleteGoods(goodsId);
        log.info("删除商品成功, 商品名称: {}", operateDTO.getName());
        return Result.ok(operateDTO);
    }

    /**
     * 批量删除选中商品
     *
     * @param command 批量删除命令
     * @return 操作结果
     */
    @DeleteMapping("/selected")
    @Operation(summary = "批量删除选中商品", description = "主要用于商品管理")
    public Result<List<DeleteGoodsOperateDTO>> deleteGoodsSelected(@RequestBody BatchDeleteCommand command) {
        log.info("批量删除选中商品, 请求参数: {}", command);
        List<DeleteGoodsOperateDTO> operateDTOList = goodsService.deleteGoodsSelected(command);
        log.info("批量删除商品成功, 删除数量: {}", operateDTOList.size());
        return Result.ok(operateDTOList);
    }
}
